package com.usian.controller;

import com.usian.pojo.Student;
import com.usian.service.StudentService;
import com.usian.service.impl.StudentServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/student")
@Controller
public class StudentController {

        @Autowired
        private StudentService studentService;

        private final static Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);

        @RequestMapping("/toadd")
        public String toadd(){
            return "add";
        }

        @RequestMapping("/findAll")
        public String findAll(Model model){
            List<Student> list = studentService.findAll();
            model.addAttribute("list",list);
            return "index";
        }

        @RequestMapping("/addStu")
        public String addStu(/*HttpServletRequest request, HttpServletResponse response ,*/ Student student){
            logger.info("=================用户添加成功controller");
            studentService.addStu(student);
     /*   try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";*/
            return "redirect:/student/findAll";
        }

        @RequestMapping("/delStu")
        public String delStu(/*HttpServletRequest request, HttpServletResponse response , */Integer id){
            studentService.delStu(id);
      /*  try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
            return "redirect:/student/findAll";
        }

        @RequestMapping("/toupdate")
        public String toupdate(Integer id,Model model){
            Student student = studentService.findById(id);
            model.addAttribute("student",student);
            return "update";
      /*  ModelAndView view = new ModelAndView();
        view.addObject("student",student);
        return view;*/
        }

        @RequestMapping("/updateStu")
        public String updateStu(/*HttpServletRequest request, HttpServletResponse response ,*/Student student){
            studentService.updateStu(student);
       /* try {
            response.sendRedirect(request.getContextPath()+"findAll");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
            return "redirect:/student/findAll";
        }

    }
